package com.edu.common.util;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @author ruixj
 * @DESC 字符串处理的相关函数
 */
public final class SimpleStringUtil {
	
	private static final String maskChar = "*";
	
	/**
	 * @DESC 生成多位随即数字字符串
	 * @param strLen 生成字符串的长度
	 * @return
	 */
	public static String getRandomString(int strLen){
		StringBuffer buf = new StringBuffer();
		for(int i=0; i<strLen; i++){
			int num = (int)Math.round(Math.random() * 9);
			buf.append(num);
		}
		return buf.toString();
	}
	
	/**
	 * @DESC 按照点截取字符串
	 * @param sourceStr 
	 * @return
	 */
	public static String reMovesuffix(String sourceStr){
		int pointIndex = sourceStr.indexOf(".");
		if(pointIndex!=-1)
			return sourceStr.substring(0, pointIndex);
		return sourceStr;
	}
	
	/**
	 * @DESC 对字符串进行掩码处理
	 * @param source 原字符串
	 * @param start 掩码开始位置
	 * @param len 掩码长度
	 * @return
	 */
	public static String stringMask(String source, int start, int len){
		if(null == source) return "";
		if(source.length() < start) return source;
		if(len < 0 || source.length() < start + len) len = source.length() - start;
		String strHead = source.substring(0, start);
		String strTear = source.substring(start + len);
		StringBuffer target =  new StringBuffer(strHead);
		for(int i=0; i<len; i++){
			target.append(maskChar);
		}
		target.append(strTear);
		return target.toString();
	}
	
	/**
	 * @DESC 对字符串进行掩码处理, 对中间1/3字符进行掩码
	 * @param source 原字符串
	 * @return
	 */
	public static String stringMask(String source){
		if(null == source) return "";
		int len = source.length();
		int start = Math.round(len / 3);
		int end = (int)Math.floor(len * 2 / 3);
		String strHead = source.substring(0, start);
		String strTear = source.substring(end);
		StringBuffer target =  new StringBuffer(strHead);
		for(int i=0; i<end - start; i++)
			target.append(maskChar);
		target.append(strTear);
		return target.toString();
	}
	
	/**
	 * @DESC 字符串补空格操作
	 * @param str 原字符串
	 * @param strLength 
	 * @param rc 补充方向
	 * @param chr 补充字符
	 * @return
	 */
	public static String addStr(String str, int strLength, String rc, String chr) {
		int strLen = str.length();
		StringBuffer buf = new StringBuffer();
		if (strLen < strLength) {
			int tempLen = strLength - strLen;
			for(int i=0; i<tempLen; i++)
				buf.append(chr);
		}
		if("L".equals(rc)){
			buf.append(str);
		}else{
			buf.insert(0, str);
		}
		return buf.toString();
	}

	public static String getMD5Str(String str) {
		MessageDigest messageDigest = null;

		try {
			messageDigest = MessageDigest.getInstance("MD5");

			messageDigest.reset();

			messageDigest.update(str.getBytes("UTF-8"));
		} catch (NoSuchAlgorithmException e) {
			System.out.println("NoSuchAlgorithmException caught!");
			System.exit(-1);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}

		byte[] byteArray = messageDigest.digest();

		StringBuffer md5StrBuff = new StringBuffer();

		for (int i = 0; i < byteArray.length; i++) {
			if (Integer.toHexString(0xFF & byteArray[i]).length() == 1)
				md5StrBuff.append("0").append(Integer.toHexString(0xFF & byteArray[i]));
			else
				md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));
		}

		return md5StrBuff.toString();
	}
	public static void main(String[] args) {
		System.out.println(addStr("1111", 10, "L", "0"));
	}
}
